﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BLL;

namespace ProjectStudent
{
    public partial class StudentManage : Form
    {
        public StudentManage()
        {
            InitializeComponent();
            dataGridView1.AutoGenerateColumns = false; // 禁止自动生成列
            LoadStudentPage();

        }
        StudentBLL studentBLL = new StudentBLL();
        private void btnStudentData_Click(object sender, EventArgs e)
        {
            GetAllStudent();
        }
        private void GetAllStudent()
        {
            DataTable dt = studentBLL.GetStudent();
            dataGridView1.DataSource = dt;
        }
        private void LoadStudentPage()
        {

            int total = studentBLL.StudentCount("");
            lb_total.Text = total.ToString();
            int pageSize = Convert.ToInt32(comboBox1.Text);//每页数量
            int totalPages = (int)Math.Ceiling((double)total / pageSize);//总页数
            lb_pageCount.Text = totalPages.ToString();
            int pageNumber = Convert.ToInt32(txtCurrentPage.Text);//当前页
            DataTable dt = studentBLL.GetStudentPage(pageNumber, pageSize, "");
            dataGridView1.DataSource = dt;
        }

        private void btnHome_Click(object sender, EventArgs e)
        {
            txtCurrentPage.Text = "1";
            LoadStudentPage();
        }

        private void btnPre_Click(object sender, EventArgs e)
        {
            int pageNumber = Convert.ToInt32(txtCurrentPage.Text);
            if (pageNumber > 1)
            {
                pageNumber--;
                txtCurrentPage.Text = pageNumber.ToString();
            }
            else
            {
                MessageBox.Show("已经是第一页了");
            }
            LoadStudentPage();
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            int pageNumber = Convert.ToInt32(txtCurrentPage.Text);
            int totalpage = Convert.ToInt32(lb_pageCount.Text);
            if (pageNumber < totalpage)
            {
                pageNumber++;
                txtCurrentPage.Text = pageNumber.ToString();
            }
            else
            {
                MessageBox.Show("已经是最后一页了");
            }
            LoadStudentPage();
        }

        private void btnEnd_Click(object sender, EventArgs e)
        {
            int totalpage = Convert.ToInt32(lb_pageCount.Text);
            txtCurrentPage.Text = totalpage.ToString();
            LoadStudentPage();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtCurrentPage.Text = "1";
            LoadStudentPage();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            StudentForm studentForm = new StudentForm(null);
            if (studentForm.ShowDialog() == DialogResult.OK)
            {
                if (studentBLL.AddStudent(studentForm.studentNo, studentForm.studentName, studentForm.gender, Convert.ToDateTime(studentForm.birthDate), Convert.ToDateTime(studentForm.enrollmentDate), Convert.ToInt32(studentForm.classID), studentForm.status))
                {
                    MessageBox.Show("新增成功！");
                    LoadStudentPage();
                }
                else
                {
                    MessageBox.Show("新增失败！");
                }
                   


            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            DataRowView dataRow = dataGridView1.CurrentRow.DataBoundItem as DataRowView;
            StudentForm studentForm = new StudentForm(dataRow.Row);
 
            if (studentForm.ShowDialog() == DialogResult.OK)
            {
                if (studentBLL.UpdateStudent(studentForm.studentID, studentForm.studentNo, studentForm.studentName, studentForm.gender, Convert.ToDateTime(studentForm.birthDate), Convert.ToDateTime(studentForm.enrollmentDate), Convert.ToInt32(studentForm.classID), studentForm.status))
                {
                    MessageBox.Show("编辑成功！");
                    LoadStudentPage();
                }
                else
                {
                    MessageBox.Show("编辑失败！");
                }
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if(dataGridView1.CurrentRow == null)
            {
                MessageBox.Show("请先选择要删除的用户！");
                return;
            }
            DataRow rowView = dataGridView1.CurrentRow.DataBoundItem as DataRow;
            int StudentID = (int)rowView["StudentID"];
            if (MessageBox.Show("确认要删除该用户吗？", "确认删除", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                if (studentBLL.DeleteStudent(StudentID))
                {
                    MessageBox.Show("删除成功！");
                    LoadStudentPage();
                }
                else
                {
                    MessageBox.Show("删除失败！");
                }
            }
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            LoadStudentPage();
        }

        private void btnExport_Click(object sender, EventArgs e)
        {
            // 创建一个 DataTable 并将 DataGridView 的数据复制到其中
            DataTable dataTable = new DataTable();
            foreach (DataGridViewColumn column in dataGridView1.Columns)
            {
                dataTable.Columns.Add(column.Name);
            }
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                DataRow score = dataTable.NewRow();
                for (int i = 0; i < dataGridView1.Columns.Count; i++)
                {
                    if (row.Cells[i].Value != null)
                    {
                        score[i] = row.Cells[i].Value.ToString();
                    }
                }
                dataTable.Rows.Add(score);
            }

            // 选择保存文件的路径
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Filter = "CSV 文件 (*.csv)|*.csv";
            saveFileDialog.Title = "保存为 CSV 文件";
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                string filePath = saveFileDialog.FileName;
                studentBLL.ExportToCsv(dataTable, filePath);
                MessageBox.Show("导出成功！");
            }
            else
            {
                MessageBox.Show("导出失败！");
            }
            //SaveFileDialog saveFileDialog = new SaveFileDialog();
            //saveFileDialog.Filter = "CSV文件 (*.csv)|*.csv";
            //saveFileDialog.Title = "请选择导出CSV文件保存位置";
            //if(saveFileDialog.ShowDialog() == DialogResult.OK)
            //{
            //    string fileName = saveFileDialog.FileName;
            //    if(studentBLL.ExportStudentToCsv(fileName))
            //    {
            //        MessageBox.Show("导出成功！");
            //    }
            //    else
            //    {
            //        MessageBox.Show("导出失败！");
            //    }
            //}
        }

        private void btnImport_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "CSV文件 (*.csv)|*.csv";
            openFileDialog.Title = "请选择导出CSV文件保存位置";
            if(openFileDialog.ShowDialog() == DialogResult.OK)
            {
                string filePath = openFileDialog.FileName;
                if(studentBLL.ImportStudentFromCsv(filePath))
                {
                    MessageBox.Show("导入成功！");
                    LoadStudentPage(); 
                }
                else
                {
                    MessageBox.Show("导入失败！");
                }
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            string keyword = txtSearch.Text;
            if(string.IsNullOrEmpty(keyword))
            {
                GetAllStudent();
                return;
            }
            DataTable dt = studentBLL.SearchHaveKeyWord(keyword);
            if(dt.Rows.Count>0)
            {
                dataGridView1.DataSource = dt;
            }
            else
            {
                dataGridView1.DataSource = null;
            }
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            txtSearch.Text = "";
        }
    }
}
